//
//  YPCoreBase_vc.swift
//  YPSwift
//  基础控制器
//  Created by ogawa on 2020/4/28.
//  Copyright © 2020 ogawa. All rights reserved.
//

import UIKit
import RxSwift

open class YPCoreBase_vc: UIViewController {
    
    public var disposeBag = DisposeBag()
    
    public lazy var errorPageView: YPErrorPageView = {
       let view = YPErrorPageView()
       view.tag = YPCoreM.ErrorPageViewTag
       return view
    }()
    
    public func showErrorPage(_ isHiden:Bool) {
        self.errorPageView.isHidden = isHiden
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()

        if #available(iOS 11.0, *) {
            UIScrollView.appearance().contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }
        
        /// 视图设置
        self.makeUI()
        /// 界面主题设置
        self.setViewTheme()
        /// 绑定到viewmodel 设置
        self.bindToViewModel()
        
    }
    
    open override  func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        configNavigationBar()
    }
    
    open override  func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    open override  func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    /**
     自定义leftBarButtonItem
     */
    open func customLeftBarButtonItem() {
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "iconfontback"), style: .plain, target: self, action: #selector(backToView))
    }

    func configNavigationBar() {
         guard let navi = navigationController else { return }
         if navi.visibleViewController == self {
             navi.barStyle(.theme)
             navi.disablePopGesture = false
             navi.setNavigationBarHidden(false, animated: true)
             if navi.viewControllers.count > 1 {
                navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "iconfontback"), style: .plain, target: self, action: #selector(backToView))
             }
         }
     }
    
    /**
     自定义返回上级界面
     */
    @objc open  func backToView() {
        _ = self.navigationController?.popViewController(animated: true)
    }
    
    // MARK: 通用设置部分
    
    /**
     界面基础设置
     */
    open func makeUI() {
    }
    
    /**
     app 主题 设置
     */
    open func setViewTheme() {
    }
    
    /**
     绑定到viewmodel 设置
     */
    open func bindToViewModel() {   
    }
    
}
